import streamlit as st
import re
import data.data as dd
import time
import base64
import string
from typing import Union

st.set_page_config(
    page_title="注册页面",
    page_icon="🤖"
)

st.html("""
    <style>
        #root > div:nth-child(1) > div.withScreencast > div > div > div > section > div.block-container.st-emotion-cache-13ln4jf.ea3mdgi5 > div > div > div > div.st-emotion-cache-ocqkz7.e1f1d6gn5 > div.st-emotion-cache-1bt9eao.e1f1d6gn3 > div > div > div > div > div > button {
            border:0;
            background-color:transparent;
         }
         #root > div:nth-child(1) > div.withScreencast > div > div > div > section > div.block-container.st-emotion-cache-13ln4jf.ea3mdgi5 > div > div > div > div.st-emotion-cache-ocqkz7.e1f1d6gn5 > div.st-emotion-cache-1bt9eao.e1f1d6gn3 > div > div > div > div > div > button > div > p {
            font-size: 1.0rem
         }
         #root > div:nth-child(1) > div.withScreencast > div > div > div > section > div.block-container.st-emotion-cache-13ln4jf.ea3mdgi5 > div > div > div > div.st-emotion-cache-ocqkz7.e1f1d6gn5 > div.st-emotion-cache-s9khlj.e1f1d6gn3 > div > div > div > div > div > button {
            border:0;
            background-color:transparent;
         }
         #root > div:nth-child(1) > div.withScreencast > div > div > div > section > div.block-container.st-emotion-cache-13ln4jf.ea3mdgi5 > div > div > div > div.st-emotion-cache-ocqkz7.e1f1d6gn5 > div.st-emotion-cache-s9khlj.e1f1d6gn3 > div > div > div > div > div > button > div > p{
         font-size: 0.8rem}
    </style>
"""
)
st.header('注册你的账号')
username = st.text_input("请输入手机号")
password = st.text_input("请输入你的密码",type="password")
repass = st.text_input("请再次输入你的密码",type="password")

col1, col2 = st.columns([7,2])
with col1:
    registerFlag = st.button("点击注册")
with col2:
    loginFlag = st.button("已有账号，点击登录")

PHONE_PATTERN = re.compile("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[0,5-9])|(18[0,5-9])|(19[0-9]))\d{8}$")

def is_phone_number(phone: Union[str, int]) -> bool:
    phone = str(phone)
    if not phone.isdigit():
        return False
    return bool(PHONE_PATTERN.match(phone))


# 强密码表示字符串中同时含有数字、小写字母、大写字母、标点符号这4类字符,而弱密码表示字符串中仅包含4类字符中的一种
def inspect(str):
    dig = string.digits
    lower = string.ascii_lowercase
    upper = string.ascii_uppercase
    pun = string.punctuation
    i, j, k, s = 0, 0, 0, 0
    sum = 0
    for x in str:
        if x in dig:
            i += 1
        elif x in lower:
            j += 1
        elif x in upper:
            k += 1
        elif x in pun:
            s += 1
    i1, j1, k1, s1 = \
    int(bool(i)), int(bool(j)), int(bool(k)), int(bool(s))
    sum = i1 + j1 + k1 + s1
    if sum == 0:
        return 'Wrong password format'
    elif sum == 1:
        return 'weak password需要小写、大写、数字、特殊字符组合'
    elif sum == 2:
        return 'Medium low password'
    elif sum == 3:
        return 'Medium high password'
    elif sum == 4:
        return 'high password'

def register(username,password,repass):
    # 1,校验三个信息是否填写
    if username and password and repass:
        #2,校验用户名的长度是否为11位，并且是否为手机号 正则表达式
        if is_phone_number(username):
            if password == repass:
                if inspect(password) == 'high password':
                    dd.add_user(username, password)
                    st.success("注册成功")
                    time.sleep(2)
                    st.switch_page("login.py")
                else:
                    st.error("密码要同时含有数字、小写字母、大写字母、标点符号这4类字符")
            else:
                st.error("两次密码不一致")
        else:
            st.error("手机号格式不正确")
    else:
        st.error("请填写相关信息")
if registerFlag:
    register(username,password,repass)
if loginFlag:
    st.switch_page("login.py")

def main_bg(main_bg):
    main_bg_ext = "png"
    st.markdown(
        f"""
         <style>
         .stApp {{
             background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()});
             background-size: cover
         }}
         </style>
         """,
        unsafe_allow_html=True
    )

# 调用
main_bg('images/login6.png')
